using System;
using System.IO;
using System.Collections.Generic;
using NDesk.Options;
using SevenZip.Compression.LZMA;
using DLCPacker = AmaroK86.MassEffect3.DLCPack;
using DLCUnpacker = AmaroK86.MassEffect3.DLCUnpack;

namespace AmaroK86.MassEffect3.DLCManager
{
	class MainClass
	{
        private static string GetExecutableName()
        {
            return Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
        }

		public static void Main (string[] args)
		{
            bool showHelp = false;
			int fastByte = 16;
			bool verbose = false;

            var options = new OptionSet()
            {
                {
                    "v|verbose",
                    "be verbose",
                    v => verbose = v != null
                },
                {
                    "o|overwrite",
                    "overwrite unpackaged files. Only for decompress",
                    v => DLCUnpacker.overwriteFiles = v != null
                },
                {
                    "u|nounknown",
                    "don't extract unknown files. Only for decompress",
                    v => DLCUnpacker.extractUnknowns = v == null
                },
                {
                    "f|hard",
                    "force the compression of .bik and .afc files. Only for compress",
                    v => DLCPacker.forceCompress = v != null
                },
                {
                    "n=|fastbyte",
                    "lzma fast byte size. Values are between 5 and 273\n5 = fast compression, bigger size.\n273 = slow compression, smaller size.\nDefault:16\nOnly for compress",
                    (int v) => fastByte = v
                },
                {
                    "h|help",
                    "show this message and exit",
                    v => showHelp = v != null
                },
            };

            List<string> extras;

            try
            {
                extras = options.Parse(args);
            }
            catch (OptionException e)
            {
                Console.Write("{0}: ", GetExecutableName());
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName());
                return;
            }

            if (extras.Count < 1 || extras.Count > 7 || showHelp == true)
            {
                Console.WriteLine("Info: AmaroK86 Mass Effect 3 DLC sfar Manager v1.0");
                Console.WriteLine();
                Console.WriteLine("Usage: {0} [OPTIONS]+ [input_dir] output_sfar", GetExecutableName());
                Console.WriteLine("  or");
                Console.WriteLine("Usage: {0} [OPTIONS]+ input_sfar [output_dir]", GetExecutableName());
                Console.WriteLine();
                Console.WriteLine("Options:");
                options.WriteOptionDescriptions(Console.Out);
                return;
            }

			if(verbose)
			{
				DLCPacker.verbose = verbose;
				DLCUnpacker.verbose = verbose;
			}

            string inputStr = extras[0];
			if(Directory.Exists(inputStr))
			{
				string dir = inputStr;
				string outputPath;
				if(dir[dir.Length-1].CompareTo('\\') != 0)
					dir += "\\";
            	outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(dir.Substring(0,dir.Length-1), ".sfar");

				Console.WriteLine("Packing directory: {0}",dir);

				SevenZipHelper.SetFastByte(fastByte);

				DLCPacker.Compress(dir, outputPath);
            	Console.WriteLine("Output file: {0}",outputPath);
			}
			else if(File.Exists(inputStr))
			{
                var outputPath = extras.Count > 1 ? extras[1] : Path.ChangeExtension(inputStr, null);
				Console.WriteLine("Unpacking {0}",inputStr);
				DLCBase dlcUnpack = new DLCBase(inputStr);
				DLCUnpacker.Decompress(dlcUnpack, inputStr, outputPath);
			}
		}// End of Main()
	}
}
